package cn.com.generaldata.os.service.common;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.constant.ReportConstant;
import cn.com.generaldata.os.dao.intf.BaseDAOInterface;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.pojo.ReportMarking;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.pojo.mvc.Report;
import cn.com.generaldata.os.pojo.mvc.Row;
import cn.com.generaldata.os.util.XMLComplexityIntf;
import cn.com.generaldata.os.util.XMLUtil;

@Component
public class MarkingService {

	@Autowired
	@Qualifier("baseDAO")
	private BaseDAOInterface<ReportMarking> reportMarkingDAO;

	public void mark4Submitting(Report report) {
		if (report == null || report.getCheckStatus() != ReportConstant.FLAG_COMMITTED
				|| report.getCheckStatus() != ReportConstant.FLAG_PROVINCE_PASSED_UNCOMMITTED) {
			// throw new
			// CommonException(ExceptionConstant.MSG_ILLEGAL_PARAMETER);
			return;
		}
		mark(report);
	}

	public void mark4Passing(Report report) {
		if (report == null || report.getCheckStatus() != ReportConstant.FLAG_PASSED || report.getCheckStatus() != ReportConstant.FLAG_PROVINCE_PASSED_COMMITTED) {
			// throw new
			// CommonException(ExceptionConstant.MSG_ILLEGAL_PARAMETER);
			return;
		}
		mark(report);
	}

	public void mark4Rejecting(Report report) {
		if (report == null || report.getCheckStatus() != ReportConstant.FLAG_REJECTED || report.getCheckStatus() != ReportConstant.FLAG_PROVINCE_REJECTED) {
			// throw new
			// CommonException(ExceptionConstant.MSG_ILLEGAL_PARAMETER);
			return;
		}
		mark(report);
	}

	private void mark(Report report) {
		if (report == null || StringUtils.isBlank(report.getReportOperatorId()) || StringUtils.isBlank(report.getReportId()) || report.getCheckStatus() == 0) {
			// throw new
			// CommonException(ExceptionConstant.MSG_ILLEGAL_PARAMETER);
			return;
		}

		User operator = new User();
		operator.setId(report.getReportOperatorId());

		cn.com.generaldata.os.pojo.Report markingReport = new cn.com.generaldata.os.pojo.Report();
		markingReport.setId(report.getReportId());

		ReportMarking reportMarking = new ReportMarking(UUID.randomUUID().toString().replaceAll("-", ""), operator, markingReport, Byte.valueOf(
				report.getCheckStatus()).shortValue(), new Date(), XMLUtil.marshalComplex(report, new XMLComplexityIntf() {

			@SuppressWarnings("rawtypes")
			public Map<String, Class> generateAliasMap() {
				Map<String, Class> map = new HashMap<String, Class>();
				map.put("Report", Report.class);
				map.put("Row", Row.class);
				return map;
			}

		}));

		reportMarkingDAO.save(reportMarking);
	}

	public void setReportMarkingDAO(BaseDAOInterface<ReportMarking> reportMarkingDAO) {
		this.reportMarkingDAO = reportMarkingDAO;
	}

	// 自加

	public List<ReportMarking> showMarking(String reportId) {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.DESC);
		filter.setSortBy("markingDate");

		filter.addRule(new FilterRule(FilterConstant.EQUAL, "report.id", reportId));
		return reportMarkingDAO.find(ReportMarking.class, filter, null);
	}

	public ReportMarking queryReportMarkingById(String id) {
		if (id != null) {
			return reportMarkingDAO.get(ReportMarking.class, id);
		}
		return null;
	}

}
